package com.chinasoft.news.controller;

import com.chinasoft.news.pojo.ArticleCate;
import com.chinasoft.news.pojo.Code;
import com.chinasoft.news.pojo.Result;
import com.chinasoft.news.service.ArticleCateService;
import com.sun.org.apache.regexp.internal.RE;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@AllArgsConstructor
public class ArticleCateController {


    ArticleCateService articleCateService;

    @GetMapping("/my/article/cates")
    public Result getArticleCate(){
        Result result = new Result(0, "selectAll is ok",null, Code.SelectCateOK);
        List<ArticleCate> articleCates = articleCateService.getArticleCate();
        result.setData(articleCates);
        return  result;
    }

    @PostMapping("/my/article/addcates")
    public Result addArticle(@RequestParam("name") String name,@RequestParam("alias") String alias){
        Result result = new Result(0, "addCate is ok", null, Code.AddCateOk);
        ArticleCate cateByName = articleCateService.getCateByName(name);
        if(cateByName!=null){
            result.setStatus(1);
            result.setMessage("分类名已存在，请更换分类名");
        }
        ArticleCate cate = new ArticleCate();
        cate.setAlias(alias);
        cate.setName(name);
        cate.setIs_delete(0);
        Boolean insertCate = articleCateService.insertCate(cate);
        if(!insertCate){
            result.setStatus(1);
            result.setMessage("分类增加失败");
        }

        return result;
    }

    @GetMapping("my/article/deletecate/{id}")
    public Result deleteArticleCate(@PathVariable("id") Integer id){
        Result result = new Result(0, "delete is ok");
        Boolean deleteCate = articleCateService.deleteCate(id);
        if(!deleteCate){
            result.setStatus(1);
            result.setMessage("删除失败");
        }
        return  result;
    }

    @GetMapping("/my/article/cates/{id}")
    public Result getArticleById(@PathVariable("id") Integer id){
        Result result = new Result(0, "selectById is ok",null,Code.GetCateOk);
        ArticleCate cate = articleCateService.getCateById(id);
        if(cate==null){
            result.setStatus(1);
            result.setMessage("不存在该文章分类");
        }
        result.setData(cate);
        return result;

    }

    @PostMapping("/my/article/updatecate")
    public Result updateArticleCate(@RequestParam("name") String name,@RequestParam("alias") String alias ,@RequestParam("id") Integer id){
        Result result = new Result(0, "update is ok", null, Code.UpdateCateOK);
        ArticleCate cateByName = articleCateService.getCateByName(name);
        if(cateByName!=null){
            result.setStatus(1);
            result.setMessage("文章分类已存在，更新失败");
        }
        Boolean updateCateById = articleCateService.updateCateById(name, alias, id);
        if(!updateCateById){
            result.setStatus(1);
            result.setMessage("文章分类更新失败");
        }
        ArticleCate articleCate = new ArticleCate();
        articleCate.setAlias(alias);
        articleCate.setName(name);
        result.setData(articleCate);
        return  result;
    }



}
